﻿@{
    ViewBag.Title = "门诊病人登记";
    Layout = "~/Views/Shared/_Form.cshtml";

    var isOpenSfqr = (ViewBag.IsOpenSfqr as bool?) ?? false;
    var TZPZ_OutpatientRegister = ViewBag.TZPZ_OutpatientRegister ?? "";
}
<style>
    .rema {
        color: red;
    }
</style>
<script type="text/javascript">
    var mzhpz;
    var objblh = "";
    $(function () {
        patInfoObj = {}
        mzhpz = $("#mzhpz").val();
        if (mzhpz == "ON") {
            $("#mzh").attr("disabled", "disabled").css("background-color", "#f1f4f6");
            $('#mzh').attr("placeholder", "由系统自动生成");
            $('#mzlog').html("");
        }

        $("#ghrq").val($.getDate());

        if ('@(ViewBag.IsDefaultDoc)' == 'ON' && top.window.clients.doctorInHosBookkeep.length == 1) {
            $("#doctormc").attr("data-label", top.window.clients.doctorInHosBookkeep[0].StaffGh);
            $("#doctormc").val(top.window.clients.doctorInHosBookkeep[0].StaffName);
            $("#ksmc").val(top.window.clients.doctorInHosBookkeep[0].ksmc);
            $("#ksmc").attr("data-label", top.window.clients.doctorInHosBookkeep[0].ks);
        }
        //医生浮层
        $("#doctormc").newtouchBatchFloatingSelector({
            height: 200,
            width: 300,
            clickautotrigger: true,
            filter: function (keyword) {
                //遍历数据源，用keyword来筛选出结果
                var resultObjArr = new Array();//
                $.each(top.window.clients.doctorInHosBookkeep, function (idx, val) {
                    if ($.trim(keyword).length == 0 ||
                        (val.py && val.py.toUpperCase().indexOf(keyword.toUpperCase()) >= 0) ||
                        (val.StaffName && val.StaffName.indexOf(keyword.toLowerCase()) >= 0) ||
                        (val.gh && val.gh.indexOf(keyword.toLowerCase()) >= 0) ||
                        (val.kspy && val.kspy.indexOf(keyword.toUpperCase()) >= 0) || keyword.trim() == '') {
                        resultObjArr.push(val);
                    }
                });
                return resultObjArr;

            },
            caption: "医生科室对照",
            colModel: [
                { label: '医生名称', name: 'StaffName', widthratio: 25 },
                { label: 'StaffGh', name: 'StaffGh', widthratio: 25, hidden: true },
                { label: '科室名称', name: 'ksmc', widthratio: 25 },
                { label: 'ks', name: 'ks', widthratio: 25, hidden: true }

            ],
            itemdbclickhandler: function ($thistr) {
                $("#doctormc").val($thistr.attr("data-StaffName"));
                $("#doctormc").attr("data-label", $thistr.attr("data-StaffGh"));
                $("#ksmc").val($thistr.attr("data-ksmc"));
                $("#ksmc").attr("data-label", $thistr.attr("data-ks"));
            },
        });

        //患者来源
        $("#brly").newtouchBindSelect({
            datasource: function () {
                var resultObjArr = new Array();
                $.each($.itemDetails.getItems('PatientSource'), function () {
                    $('#brly').append('<option value="' + this.Code + '">' + this.Name + '</option>');
                });
                return resultObjArr;
            }
        });

        //紧急联系人关系
        $("#lxrgx").newtouchBindSelect({
            datasource: function () {
                var resultObjArr = new Array();
                $.each($.itemDetails.getItems('RelativeType'), function () {
                    $('#lxrgx').append('<option value="' + this.Code + '">' + this.Name + '</option>');
                });
                return resultObjArr;
            }
        });

        //治疗项目选择浮层绑定
        $('#blh').newtouchBatchFloatingSelector({
            width: 500,
            height: 200,
            caption: "选择患者",
            url: "@Url.Action("PatSearchInfo")",
            ajaxparameters: function ($thisinput) {
                var keyword = $thisinput.val().trim();
                return "keyword=" + keyword;
            },
            itemdbclickhandler: function ($thistr, $thisinput) {
                objblh = $thistr.attr('data-blh');
                GetQueryFphAjax({ patid: $thistr.attr('data-patid'), blh: objblh });
            },
            colModel: [
                 { label: '主键', name: 'patid', hidden: true },
                 { label: '病历号', name: 'blh', width: 130, align: 'left' },
                 { label: '姓名', name: 'xm', width: 120, align: 'left' },
                 { label: '出生年月', name: 'csny', hidden: true, width: 100, align: 'left' },
                 {
                     label: '性别', name: 'xb', width:70, align: 'left', formatter: function (cellvalue) {
                         return $.getGender(cellvalue);
                     }
                 },
                {
                    label: '年龄', name: 'nlshow', width: 100, align: 'center', formatter: function (cellvalue, a, b) {
                        return getAgeFromBirthTime({ begin: b.csny }).text;
                    }
                },
                 { label: 'brly', name: 'brly', align: 'left', hidden: true },
                 { label: 'zjh', name: 'zjh', align: 'left', hidden: true },
                   { label: 'brxz', name: 'brxz', align: 'left', hidden: true },
                 { label: 'kh', name: 'kh', align: 'left', hidden: true },
                 { label: 'phone', name: 'phone', align: 'left', hidden: true },
                 { label: 'dy', name: 'dy', align: 'left', hidden: true },
                 { label: 'zjlx', name: 'zjlx', align: 'left', hidden: true },
                 { label: 'sycs', name: 'sycs', align: 'left', hidden: true },
                 { label: 'dybh', name: 'dybh', align: 'left', hidden: true },
                 { label: 'lxr', name: 'lxr', align: 'left', hidden: true },
                 { label: 'lxrgx', name: 'lxrgx', align: 'left', hidden: true },
                 { label: 'lxrdh', name: 'lxrdh', align: 'left', hidden: true },
            ]
        });

        $("#lxrdh").keyupEnterEvent(function () {
            btn_Save();
        });
    });

    function btn_NocardRes() {
        $.modalOpen({
            id: "Form",
            title: "新建登记",
            url: "/PatientManage/HospiterRes/PatientBasic?T=" + new Date(),
            width: "1000px",
            height: "824px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
                $("#xlbrbz").prop('checked', false);
            }
        });
    }

    //患者查询弹出
    function GetPatSerarchView(blh) {
        $.modalOpen({
            id: "patSearch",
            title: "患者查询",
            url: "/OutpatientManage/OutpatientAccounting/SysPatEntitiesblhView?t=" + Math.random() + "&blh=" + blh,
            width: "700px",
            height: "600px",
            callBack: function (iframeId) {
                top.frames[iframeId].PatDbGrid(); //在弹出窗口事件
            }
        });
    }

    //患者查询调用
    function GetQueryFphAjax(obj) {
        //判断是否重复登记
        AllowRegh(obj.blh);
        //清空
        ClearAll();
        //获取病人基本信息
        CallbackPatientQuery(obj.blh);
    }

    //根据病历号搜索返回基本信息
    function CallbackPatientQuery(blh) {
        $.ajax({
            url: "/PatientManage/HospiterRes/GetOutpatientBasicInfo",
            data: { blh: blh },
            dataType: "json",
            async: false,
            success: function (rep) {
                if (rep.data == null) return;
                $("#brly").find("option[value='" + rep.data["brly"] + "']").attr("selected", true).trigger('change');
                var patModel = rep.data;
                objblh = patModel.blh;
                $("#xb").html($.getGender(patModel.xb));
                $("#csny").html((patModel.csny && patModel.csny.length >= 10 ? patModel.csny.substring(0, 10) : ""));
                $("#zjh").html(patModel.zjh);
                $("#jsr").html(patModel.jsr);
                $("#xm").html(patModel.xm);
                $("#nlshow").html(getAgeFromBirthTime({ begin: patModel.csny }).text);
                $("#blh").val(patModel.blh);
                $("#kh").html(patModel.kh);
                $("#patid").val(patModel.patid);
                $("#phone").html(patModel.phone);
                $("#csny").html(patModel.csny);
                $("#hiddenzjlx").val(patModel.zjlx);
                $("#xlbrbz").prop('checked', true);
                $("#brxzmc").val(patModel.brxz);
                $("#lxrgx").find("option[value='" + rep.data["lxrgx"] + "']").attr("selected", true).trigger('change');//.val(patModel.lxrgx);
                $("#lxr").val(patModel.lxr);
                $("#lxrdh").val(patModel.lxrdh);
            }
        });
    }

    function AbledSysBasicInfo() {
        $("#xm").html("");
        $("#zjh").html("");
        $("#xb").html("");
        $("#nlshow").html("");
        $("#blh").html("");
        $("#phone").html("");
        //$("#dy").html("");
        $("#kh").html("");
        $("#blxz").val("");
        $("#csny").html("");
        $("#brxzmc").val("");
        objblh = "";
        patInfoObj = {};
    }

    function ClearAll() {
        AbledSysBasicInfo();
        newtouch_globalevent_f4();

        if ('@(ViewBag.IsDefaultDoc)' == 'ON' && top.window.clients.doctorInHosBookkeep.length == 1) {
            $("#doctormc").attr("data-label", top.window.clients.doctorInHosBookkeep[0].StaffGh);
            $("#doctormc").val(top.window.clients.doctorInHosBookkeep[0].StaffName);
            $("#ksmc").val(top.window.clients.doctorInHosBookkeep[0].ksmc);
            $("#ksmc").attr("data-label", top.window.clients.doctorInHosBookkeep[0].ks);
        }
    }

    function newtouch_event_f4() {
        AbledSysBasicInfo();
        $("#patid").val("");
        $("#xlbrbz").prop('checked', true);
    }

    function newtouch_globalevent_f6() {
        btn_Save();
    }

    function checkNotNull() {
        var patid = $("#patid").val();
        if (!patid) {
            $.modalAlert("患者基本信息不存在，无法保存", 'warning');
            return false;
        }
        if (objblh === "" || !objblh) {
            $.modalAlert("患者基本信息不存在，无法保存", 'warning');
            return false;
        }
        var validator = $('#form1').validate();
        validator.settings = {
            rules: {
                blh: { required: true },
                mzh: { required: true },
                ksmc: { required: true },
                doctormc: { required: true },
                ghrq: { required: true },
                lxr: { maxlength: 10 },
                lxrdh: { isPhone: true }
            },
            messages: {
                blh: { required: "病历号必须填写" },
                mzh: { required: "门诊号必须填写" },
                ksmc: { required: "科室必须选择" },
                doctormc: { required: "医生必须选择" },
                ghrq: { required: "登记日期必须选择" },
                lxrdh: { isPhone: "电话格式不正确" }
            },
            showErrors: function (errorMap, errorList) {
                if (!$.isEmptyObject(errorList)) {
                    if (Array.isArray(errorList) && errorList.length == 0) {
                        return;
                    }
                    $.modalAlert(errorList[0].message, 'warning');
                }
            }
        }
        if (mzhpz === "OFF" && !$("#mzh").val()) {
            $.modalAlert("门诊号必须填写", 'warning');
            return;
        }
        if (!validator.form()) {
            return false;
        }
        return true;
    }

    function btn_Save() {
        var result = checkNotNull();
        if (result) {
            var data = $("#form1").formSerialize();
            data["csny"] = $("#csny").html();
            data["ys"] = $("#doctormc").attr("data-label");
            data["brxz"] = $("#brxzmc").val();
            data["ks"] = $("#ksmc").attr("data-label");
            data["xm"] = $("#xm").html();
            data["blh"] = objblh;
            data["xb"] = $.getGenderCode($("#xb").html());
            data["zjlx"] = $("#hiddenzjlx").val();
            data["zjh"] = $("#zjh").html();
            data["kh"] = $("#kh").html();
            $.submitForm({
                url: "/PatientManage/HospiterRes/InpatientSubmit",
                loading: "正在提交数据...",
                param: data,
                successwithtipmsg: false,
                success: function (data) {
                    var message = "";
                    if ('@(TZPZ_OutpatientRegister.ToString())' !== '') {
                        var mzh = data.data;
                        if (data.message === "添加成功") {
                            message = "门诊登记成功！点击[确认]进行门诊记账";
                        }
                        else if (data.message === "修改成功") {
                            message = "门诊登记修改成功！点击[确认]进行门诊记账";
                        }
                        ClearAll();
                        $.modalConfirm(message, function (flag) {
                            if (flag) {
                                top.top.$.Newtouchtab.triggerMenuItemClick({ m1: '门诊管理', m2: '@(TZPZ_OutpatientRegister.ToString())' });
                                sessionStorage.setItem("mzh", mzh);
                                return true;
                            }
                        });
                    } else {
                        if (data.message === "添加成功") {
                            message = "门诊登记成功";
                        }
                        else {
                            message = "门诊登记修改成功";
                        }
                        ClearAll();
                        $.modalAlert(message, 'success');
                        return;
                    }
                }
            });
        }
    }

    ///判断今天是否重复登记
    function AllowRegh(p) {
        $.ajax({
            url: "@Url.Action("AllowRegh")",
            data: { blh: p },
            //dataType: "json",
            success: function (rep) {
                if (rep == "true") {
                    $.modalConfirm("该患者今天已存在登记，是否再次登记？", function (flag) {
                        if (!flag) {
                            ClearAll();
                            return;
                        }
                    });
                }
            }
        });
    }
</script>
<form id="form1">
    <div class="rows" style="margin-bottom: 1%;" id="basicInfo">
        <div class="panel panel-default" style="margin-bottom:0;">
            <div class="panel-heading navb-bg">
                患者基本信息
            </div>
            <table class="form" style="margin-left: 10px;">
                <tr>
                    <th class="formTitle"><span class="rema">*</span><label id="lbl_kh">病历号/姓名：</label> </th>
                    <td class="formValue" colspan="2">
                        <input type="text" id="blh" name="blh" class="form-control" placeholder="最少1位字符" />
                    </td>
                    <td colspan="2">
                        <button type="button" class="btn btn-primary" id="noCardRes" value="新建" onclick="btn_NocardRes()">
                            新建
                        </button>
                        &nbsp;&nbsp;
                        <input type="button" class="btn btn-default btn-md btn-default-color" id="btnsyy" value="查询" onclick="GetPatSerarchView($('#blh').val());" />
                    </td>
                    <td class="formTitle">
                        姓名：
                    </td>
                    <td class="formValue">
                        <label id="xm"></label>
                    </td>
                    <td class="formTitle">性别：</td>
                    <td class="formValue">
                        <label id="xb"></label>
                    </td>
                    <td class="formTitle">年龄：</td>
                    <td class="formValue">
                        <label id="nlshow"></label>
                    </td>
                    <td class="formTitle">
                        <label id="zjlx">身份证号：</label>
                        <input hidden="hidden" id="hiddenzjlx" />
                    </td>
                    <td class="formValue" colspan="2">
                        <label id="zjh"></label>
                    </td>
                </tr>
                <tr>
                    <td class="formTitle">手机：</td>
                    <td class="formValue"><label id="phone"></label></td>
                    <td class="formTitle">
                        <label>卡号：</label>
                    </td>
                    <td class="formValue">
                        <label id="kh"></label>
                    </td>
                    <td class="formValue">
                        <input type="hidden" id="patid" />
                        <input type="hidden" id="brxzmc" />
                        <label id="csny" hidden="hidden"></label>
                        <input type="hidden" id="mzhpz" value="@ViewBag.mzhpz" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div id="InHospitalInfo" class="rows" style="margin-bottom: 1%;">
        <div class="panel panel-default" style="margin-bottom:0;">
            <div class="panel-heading navb-bg">门诊基本信息</div>
            <table class="form" style="width:98%;border:0">
                <tr>
                    <td class="formTitle"><span id="mzlog" class="rema">*</span>门诊号：</td>
                    <td class="formValue"><input type="text" id="mzh" class="form-control form-an" /></td>
                    <td class="formTitle"><span class="rema">*</span>医生：</td>
                    <td class="formValue"><input type="text" id="doctormc" name="doctormc" class="form-control form-an formClearIgnore" /></td>
                    <td class="formTitle"><span class="rema">*</span>登记日期：</td>
                    <td class="formValue">
                        <input id="ghrq" type="text" name="ghrq" class="form-control input-wdatepicker form-an formClearIgnore"
                               onclick="WdatePicker({ dateFmt: 'yyyy-MM-dd'})" />
                    </td>
                    <td class="formTitle">患者来源：</td>
                    <td class="formValue formDdlSelectorTd">
                        <select class="form-control form-an-end" id="brly" name="brly">
                            <option value="">==请选择==</option>
                        </select>
                    </td>
                    <td class="formValue" style="padding-top: 1px; left:10px">
                        <div class="ckbox">
                            <input id="xlbrbz" name="xlbrbz" class="formClearIgnore" type="checkbox" checked="checked"><label for="xlbrbz">是否复诊</label>
                        </div>
                        <input type="hidden" id="ksmc" name="ksmc" class="form-control form-an newtouch_Readonly" />
                    </td>
                </tr>
                <tr>
                    <td class="formTitle">紧急联系人：</td>
                    <td class="formValue"><input type="text" name="lxr" id="lxr" class="form-control form-an" /></td>
                    <td class="formTitle">联系人关系：</td>
                    <td class="formValue formDdlSelectorTd">
                        <select id="lxrgx" name="lxrgx" class="form-control form-an">
                            <option value="">==请选择==</option>
                        </select>
                    </td>
                    <td class="formTitle">联系人电话：</td>
                    <td class="formValue">
                        <input id="lxrdh" type="text" name="lxrdh" class="form-control form-an" />
                    </td>
                    <td class="formTitle">
                        推荐人：
                    </td>
                    <td class="formValue">
                        <input id="tjr" type="text" name="tjr" class="form-control form-an-end" />
                    </td>
                    <td class="formValue"></td>
                </tr>
            </table>
        </div>
    </div>
    @Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel
{
    ShowKeyList = new int[] { 4, 6 },
    F6Text = "登记"
})
</form>
